#ifndef GPIO_H
#define GPIO_H

#define IN  0
#define OUT 1

typedef struct GPIO {
	int portnum;
	int export_fd;
	int unexport_fd;
	int direction_fd;
	int value_fd;
} GPIO;

/*
 * describe: open one gpio port and save result in *gpio
 *
 * gpio: GPIO pointer, save open result
 * portnum: which gpio port you want to use
 *
 * return: return value same as open() API
 */
int gpio_open(struct GPIO* gpio, int portnum);

/*
 * describe: close the gpio port when finish using
 *
 * gpio: GPIO pointer, which gpio you want to close
 */
void gpio_close(struct GPIO* gpio);

/*
 * descrite: set the gpio port direction
 * 
 * gpio: GPIO pointer, the gpio port you want to set
 * direction: can be either IN or OUT
 *
 * return: same as write() API
 */
int gpio_set_direction(struct GPIO* gpio, int direction);

/*
 * descrite: write a value to a gpio port
 *
 * gpio: GPIO pointer, the gpio port you want write to
 * value: value you want to write either 0 or 1
 *
 * return: save as write() API
 */
int gpio_write(struct GPIO* gpio, int value);

/*
 * describe: read one gpio port's value
 *
 * gpio: GPIO pointer, the gpio you want to read value from
 *
 * return: -1 on error, 0 or 1 is the port value
 */
int gpio_read(struct GPIO* gpio);

/*
 * describe: hold gpio with value for a given time
 *
 * value: the gpio value, 0 or 1
 * msec: how many time you want to hold in million seconds
 */
void gpio_hold(struct GPIO* gpio, int value, int msec);

#endif //GPIO_H
